#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>                         
#include <unistd.h>  
#include <fcntl.h>
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>
#include <time.h>
 
//来自博文： https://blog.csdn.net/sinat_16643223/article/details/120211926
 
int main()
{
    while(1)   //这里是为了让服务器循环发送数据，因为服务器端发送完一次数据会关闭连接，所以循环 
               //while(1)要从建立连接开始
   { 
     // 创建通信端点：套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);  
    printf("creat socket\r\n");
    // 设置服务器地址结构体  
    struct sockaddr_in server_addr;  
    bzero(&server_addr,sizeof(server_addr)); // 初始化服务器地址  
    server_addr.sin_family = AF_INET;   // IPv4  
    server_addr.sin_port = htons(8001); // nginx服务器监听的端口 
    inet_pton(AF_INET, "192.168.42.108", &server_addr.sin_addr);   // 服务器ip  
    printf("server addr has ready\r\n");
     // 主动连接服务器  
     int bytes_received=0;
    int filesaved=0;  
    
    int err_log = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));  
    if(err_log != 0)  
    {  
        perror("connect"); 
         printf("connect has error\r\n");
        close(sockfd);  
        return -1;  
    }
    else{
        printf("connect has ready\r\n");
    }
    
    //http请求报文包
//        char send_buf[] = 
//        "GET /GetRoundData.html HTTP/1.1\r\n"
//        "Accept: text/html,image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*\r\n"
//        "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3\r\n"
//        "User-Agent: Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)\r\n"
//        "Accept-Encoding: gzip, deflate\r\n"
//        "Host: 192.168.254.66:8000\r\n"
//        "Connection: Keep-Alive\r\n"
//        "\r\n";

    char send_buf[] = 
        "GET /cgi-bin/configManager.cgi?action=setConfig&Encode[0].MainFormat[0].Video.FPS=20 HTTP/1.1\r\n"
        "Host: 192.168.42.108\r\n"
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"
        "Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2\r\n"
        "Accept-Encoding: gzip, deflate\r\n"
        "Connection: keep-alive\r\n"
        "Authorization: Basic YWRtaW46MTIzNDU2\r\n"
        "Upgrade-Insecure-Requests: 1\r\n"
        "Cookie: VSLangCookie30=SimpChinese; username=admin; showIVSRule=false\r\n"
        "\r\n";
 
    
    //发送http请求报文包
 
    
    send(sockfd, send_buf, sizeof(send_buf)-1, 0);
    printf("message has send\r\n");
    
    //
    //int contentlengh=0;      
    //获取http响应报文
    char recv_buf[8*1024] = {0};  //recv_buf是响应信息（包括响应头部和数据体），返回值是保存的字节数
    printf("err_log=%d\n\r",err_log);
    bytes_received=recv(sockfd, recv_buf, sizeof(recv_buf), 0);
 
    printf("bytes_received=%d\r\n",bytes_received);
    //在指定位置存储服务器上文件/index.html,并在控制台打印文件内容
    //int byteRec=bytes_received-44;
    //if((byteRec))
    //{
 
//    //将服务器文件存储到客户端指定位置
//    FILE* fd=fopen("GetRoundData.json","wb+");
    
//    //int filesaved=fwrite(recv_buf,sizeof(recv_buf),1,fd);
//    filesaved=fwrite(recv_buf,1,bytes_received,fd);
//    printf("filesaved=%d",filesaved);
//    fclose(fd);
    
    sleep(2);   // 等待2秒后再次发送连接请求 
 
    }
 
    //else{
       
        //printf("data is null!!!!\n\r");
        //sleep(2);
        
    //}
 
      
    return 0;
 
}
